/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
 *
 */
import { expect } from '@jest/globals';
import fse from 'fs-extra';
import fs from 'fs-extra';
import path from 'path';

import { runHvigorAsync } from '../../common/common';
import { commonJson5Operation, commonJsonOperation } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel, CURRENT_HMOS_API,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.OpenHarmony,
    apiVersion: CURRENT_HMOS_API,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  }
};

const testFun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRootPath = project.projectRootPath();
  const module = project.getModule('entry');
  const modulePath = module.moduleRootPath();
  const commands: string[] = '--mode module -p product=default -p module=entry@default assembleHap'.split(' ');
  const outputRoot = path.resolve(modulePath, 'build/default/intermediates/syscap/default/rpcid.sc');

  // src/main/module.json5-module-deviceTypes中移除2in1，不然会报错
  const moduleJson5Path = path.resolve(modulePath, 'src/main/module.json5');
  await commonJson5Operation(moduleJson5Path, json5 => {
    json5.module = json5.module || {};
    json5.module.deviceTypes = ["default", "tablet"];
    return json5;
  });

  const result1 = await runHvigorAsync(projectRootPath, commands);
  record.addStep('首次构建，SyscapTransform为Finished', result1);
  expect(result1.stdout).toContain('BUILD SUCCESSFUL');
  expect(result1.stdout).toContain('Finished :entry:default@SyscapTransform');
  expect(fse.existsSync(outputRoot)).toEqual(true);

  const result2 = await runHvigorAsync(projectRootPath, commands);
  record.addStep('不做任何更改，再次构建，SyscapTransform为UP-TO-DATE', result2);
  expect(result2.stdout).toContain('BUILD SUCCESSFUL');
  expect(result2.stdout).toContain('UP-TO-DATE :entry:default@SyscapTransform');
  expect(fse.existsSync(outputRoot)).toEqual(true);

  fse.removeSync(outputRoot);
  const result3 = await runHvigorAsync(projectRootPath, commands);
  record.addStep('删除产物，再次构建，SyscapTransform为Finished', result3);
  expect(result3.stdout).toContain('BUILD SUCCESSFUL');
  expect(result3.stdout).toContain('Finished :entry:default@SyscapTransform');
  expect(fse.existsSync(outputRoot)).toEqual(true);

  // 修改deviceTypes-删除tablet
  await commonJson5Operation(moduleJson5Path, json5 => {
    json5.module = json5.module || {};
    json5.module.deviceTypes = ["default"];
    return json5;
  });
  const result4 = await runHvigorAsync(projectRootPath, commands);
  record.addStep('修改deviceTypes，再次构建，SyscapTransform为Finished', result4);
  expect(result4.stdout).toContain('BUILD SUCCESSFUL');
  expect(result4.stdout).toContain('Finished :entry:default@SyscapTransform');
  expect(fse.existsSync(outputRoot)).toEqual(true);

  // 创建src/main/syscap.json
  const syscapPath = path.resolve(modulePath, 'src/main/syscap.json');
  await fs.writeFile(syscapPath, "{}", 'utf8');
  await commonJsonOperation(syscapPath, json => {
    json.devices = {};
    json.devices.general = ["default"];
    json.devices.custom = [];
    json.development = {};
    json.production = {};
    return json;
  });

  const result5 = await runHvigorAsync(projectRootPath, commands);
  record.addStep('配置syscap.json，再次构建，SyscapTransform为Finished', result5);
  expect(result5.stdout).toContain('BUILD SUCCESSFUL');
  expect(result5.stdout).toContain('Finished :entry:default@SyscapTransform');
  expect(fse.existsSync(outputRoot)).toEqual(true);
};

TestCaseBuilder.builder()
  .caseName('SyscapTransform')
  .description('SyscapTransform任务增量测试用例')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.L2).build())
  .testFun(testFun)
  .load();
